package com.htl.exampro.question.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htl.exampro.base.model.PageResult;
import com.htl.exampro.base.utils.UserUtil;
import com.htl.exampro.question.mapper.AnswerMapper;
import com.htl.exampro.question.mapper.QuestionMapper;
import com.htl.exampro.question.pojo.domain.AnswerDO;
import com.htl.exampro.question.pojo.domain.QuestionDO;
import com.htl.exampro.question.pojo.query.AnswerPageParam;
import com.htl.exampro.question.pojo.vo.AnswerDetailVO;
import com.htl.exampro.question.pojo.vo.AnswerVO;
import com.htl.exampro.question.service.AnswerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 答题表 服务实现类
 * </p>
 *
 * @author hehehe
 * @since 2025-05-25
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, AnswerDO> implements AnswerService {

    @Resource
    QuestionMapper questionMapper;

    @Override
    public AnswerVO queryAnswerById(String historyId) {
        AnswerDO AnswerDO = baseMapper.selectById(historyId);
        return BeanUtil.toBean(AnswerDO, AnswerVO.class);
    }

    @Override
    public int saveAnswer(AnswerVO answerVO) {
        answerVO.setUserId(UserUtil.getUserId());
        LambdaQueryWrapper<AnswerDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AnswerDO::getUserId, UserUtil.getUserId());
        queryWrapper.eq(AnswerDO::getQuestionId, answerVO.getQuestionId());
        if (answerVO.getPaperId() != null) {
            queryWrapper.eq(AnswerDO::getPaperId, answerVO.getPaperId());
        } else {
            queryWrapper.isNull(AnswerDO::getPaperId);
        }
        if (answerVO.getExamId() != null) {
            queryWrapper.eq(AnswerDO::getExamId, answerVO.getExamId());
        } else {
            queryWrapper.isNull(AnswerDO::getExamId);
        }
        if (answerVO.getCourseId() != null) {
            queryWrapper.eq(AnswerDO::getCourseId, answerVO.getCourseId());
        } else {
            queryWrapper.isNull(AnswerDO::getCourseId);
        }
        AnswerDO answerDO = baseMapper.selectOne(queryWrapper);

        if (answerDO == null) {
            AnswerDO aDo = BeanUtil.toBean(answerVO, AnswerDO.class);
            return this.baseMapper.insert(aDo);
        } else if (!answerVO.getAnswer().equals(answerDO.getAnswer())) {
            AnswerDO aDo = new AnswerDO();
            aDo.setAnswerId(answerDO.getAnswerId());
            aDo.setAnswer(answerVO.getAnswer());
            return this.baseMapper.updateById(aDo);
        }
        return 0;
    }

    @Override
    public void saveAllAnswer(List<AnswerVO> answerVOS) {
        answerVOS.forEach(this::saveAnswer);
    }

    @Override
    public List<AnswerDetailVO> queryAnswerDetail(AnswerPageParam pageParam) {
        LambdaQueryWrapper<AnswerDO> queryWrapper = Wrappers.lambdaQuery(AnswerDO.class);
        queryWrapper.eq(pageParam.getUserId() != null, AnswerDO::getUserId, pageParam.getUserId());
        queryWrapper.eq(pageParam.getExamId() != null, AnswerDO::getExamId, pageParam.getExamId());
        queryWrapper.eq(pageParam.getPaperId() != null, AnswerDO::getPaperId, pageParam.getPaperId());
        queryWrapper.eq(pageParam.getCourseId() != null, AnswerDO::getCourseId, pageParam.getCourseId());
        queryWrapper.eq(pageParam.getIsCorrect() != null, AnswerDO::getIsCorrect, pageParam.getIsCorrect());
        List<AnswerDO> answerDOS = baseMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(answerDOS)) return new ArrayList<>();
        List<AnswerDetailVO> answerDetailVOS = BeanUtil.copyToList(answerDOS, AnswerDetailVO.class);
        List<Long> questionIds = answerDOS.stream().map(AnswerDO::getQuestionId).distinct().toList();
        List<QuestionDO> questionDOS = questionMapper.selectByIds(questionIds);
        for (QuestionDO questionDO : questionDOS) {
            for (AnswerDetailVO answerDetailVO : answerDetailVOS) {
                if (answerDetailVO.getQuestionId().equals(String.valueOf(questionDO.getQuestionId()))) {
                    answerDetailVO.setTypeId(questionDO.getTypeId());
                    answerDetailVO.setContent(questionDO.getContent());
                    answerDetailVO.setOptions(questionDO.getOptions());
                    answerDetailVO.setAnalysis(questionDO.getAnalysis());
                    answerDetailVO.setDifficulty(questionDO.getDifficulty());
                    answerDetailVO.setCorrectAnswer(questionDO.getAnswer());
                }
            }
        }
        return answerDetailVOS;
    }

    @Override
    public PageResult<AnswerVO> queryAnswerPage(AnswerPageParam pageParam) {
        Page<AnswerDO> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        LambdaQueryWrapper<AnswerDO> queryWrapper = Wrappers.lambdaQuery(AnswerDO.class);
        queryWrapper.eq(pageParam.getUserId() != null, AnswerDO::getUserId, pageParam.getUserId());
        queryWrapper.eq(pageParam.getExamId() != null, AnswerDO::getExamId, pageParam.getExamId());
        queryWrapper.eq(pageParam.getPaperId() != null, AnswerDO::getPaperId, pageParam.getPaperId());
        queryWrapper.eq(pageParam.getCourseId() != null, AnswerDO::getCourseId, pageParam.getCourseId());
        queryWrapper.eq(pageParam.getIsCorrect() != null, AnswerDO::getIsCorrect, pageParam.getIsCorrect());
        baseMapper.selectPage(page, queryWrapper);
        PageResult<AnswerVO> pageResult = new PageResult<>(page.getTotal(), page.getCurrent(), page.getSize());
        pageResult.setData(BeanUtil.copyToList(page.getRecords(), AnswerVO.class));
        return pageResult;
    }
}
